package com.zpz.framework.zpzoauth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthBeanUtils;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthExListUtil;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthPageInfoExt;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.dao.ZpzOauthFrameAccessMapper;
import com.zpz.framework.zpzoauth.dao.ZpzOauthFrameReleaseAccessMapper;
import com.zpz.framework.zpzoauth.dao.ZpzOauthOauthClientDetailsMapper;
import com.zpz.framework.zpzoauth.pojo.pe.FrameReleaseAccessPe;
import com.zpz.framework.zpzoauth.pojo.pe.FrameReleaseAccessVo;
import com.zpz.framework.zpzoauth.pojo.pe.GetAccessByRolePe;
import com.zpz.framework.zpzoauth.pojo.po.FrameAccess;
import com.zpz.framework.zpzoauth.pojo.po.FrameReleaseAccess;
import com.zpz.framework.zpzoauth.pojo.po.OauthClientDetails;
import com.zpz.framework.zpzoauth.pojo.qo.*;
import com.zpz.framework.zpzoauth.pojo.vo.GetAccessByRoleVo;
import com.zpz.framework.zpzoauth.pojo.vo.GetAccessVo;
import com.zpz.framework.zpzoauth.pojo.vo.OauthClientDetailsVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameAccessService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Service
public class ZpzOauthFrameAccessServiceImpl implements ZpzOauthFrameAccessService {
    private static final Log log = LogFactory.getLog(ZpzOauthPermissionServiceImpl.class);
    @Resource
    private ZpzOauthFrameAccessMapper zpzOauthFrameAccessMapper;
    @Resource
    private ZpzOauthOauthClientDetailsMapper zpzOauthOauthClientDetailsMapper;

    @Override
    public ZpzOauthResultMsg<List<GetAccessByRoleVo>> getAccessByDepartment(String roleCodes) {
        try {
            List<GetAccessByRolePe> ase = zpzOauthFrameAccessMapper.selectAccessByDepartment(roleCodes);
            List<GetAccessByRoleVo> getAccessByRoleVos = ZpzOauthBeanUtils.copyListProperties(ase, GetAccessByRoleVo.class);
            return ZpzOauthResultMsg.okForData(getAccessByRoleVos);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return ZpzOauthResultMsg.error();
    }

    @Override
    public ZpzOauthResultMsg<List<GetAccessByRoleVo>> getAccessByRole(String roleCodes){
        try {
            List<GetAccessByRolePe> ase = zpzOauthFrameAccessMapper.selectAccessByRole(roleCodes);
            List<GetAccessByRoleVo> getAccessByRoleVos = ZpzOauthBeanUtils.copyListProperties(ase, GetAccessByRoleVo.class);
            return ZpzOauthResultMsg.okForData(getAccessByRoleVos);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return ZpzOauthResultMsg.error();
    }
    @Resource
    private ZpzOauthFrameReleaseAccessMapper zpzOauthFrameReleaseAccessMapper;
    @Override
    public ZpzOauthResultMsg<List<FrameReleaseAccessVo>> getReleaseAccess(String clientId) {
        List<FrameReleaseAccessPe> frameReleaseAccessPes = zpzOauthFrameReleaseAccessMapper.selectReleaseAccess(clientId);
        List<FrameReleaseAccessVo> list = ZpzOauthExListUtil.copyListProperties(frameReleaseAccessPes, FrameReleaseAccessVo.class);
        return ZpzOauthResultMsg.okForData(list);
    }

    @Override
    public ZpzOauthResultMsg<OauthClientDetailsVo> getClient(String clientId) {
        OauthClientDetails t=new OauthClientDetails();
        t.setClientId(clientId);
        t.setDelFlag(0);
        List<OauthClientDetails> select = zpzOauthOauthClientDetailsMapper.select(t);
        if (select!=null&& select.size()>0){
            OauthClientDetails oauthClientDetails = select.get(0);
            OauthClientDetailsVo v=new OauthClientDetailsVo();
            BeanUtils.copyProperties(oauthClientDetails,v);
            return ZpzOauthResultMsg.okForData(v);
        }
        return ZpzOauthResultMsg.error();
    }

    /**
     * 获取权限列表
     * */
    @Override
    public ZpzOauthResultMsg<PageInfo<GetAccessVo>> getAccessPageList(HttpServletRequest request, GetAccessQo getAccessQo){

        PageHelper.startPage(getAccessQo.getPageNum(),getAccessQo.getPageSize(),true);

        FrameAccess obj=new FrameAccess();
        obj.setAccessCode(getAccessQo.getAccessCode());
        obj.setServiceId(getAccessQo.getServiceId());
        obj.setMethodUrl(getAccessQo.getMethodUrl());

        List<FrameAccess> list=zpzOauthFrameAccessMapper.selectFrameAccessList(obj);
        if (list==null){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }

        PageInfo<GetAccessVo> page = new ZpzOauthPageInfoExt<FrameAccess,GetAccessVo>().getPage(list,GetAccessVo.class);

        return ZpzOauthResultMsg.okForData(page);
    }

    /**
     * 获取放行权限列表(分页)
     * */
    @Override
    public ZpzOauthResultMsg<PageInfo<GetAccessVo>> getReleaseAccessPageList(HttpServletRequest request, GetReleaseAccessListQo getReleaseAccessListQo){
        PageHelper.startPage(getReleaseAccessListQo.getPageNum(),getReleaseAccessListQo.getPageSize(),true);

        FrameReleaseAccess obj=new FrameReleaseAccess();
        obj.setClientId(getReleaseAccessListQo.getClientId());
        obj.setAccessCode(getReleaseAccessListQo.getAccessCode());
        obj.setServiceId(getReleaseAccessListQo.getServiceId());
        obj.setMethodUrl(getReleaseAccessListQo.getMethodUrl());

        List<FrameReleaseAccess> list= zpzOauthFrameReleaseAccessMapper.selectFrameReleaseAccessList(obj);
        if (list==null){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }

        PageInfo<GetAccessVo> page = new ZpzOauthPageInfoExt<FrameReleaseAccess,GetAccessVo>().getPage(list,GetAccessVo.class);

        return ZpzOauthResultMsg.okForData(page);
    }

    /**
     * 获取权限详情
     * */
    @Override
    public ZpzOauthResultMsg<GetAccessVo> getAccessDetail(HttpServletRequest request, GetAccessDetailQo getAccessDetailQo){
        if (StringUtils.isBlank(getAccessDetailQo.getId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameAccess obj = zpzOauthFrameAccessMapper.selectByPrimaryKey(getAccessDetailQo.getId());
        if (obj==null){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }
        GetAccessVo ret=new GetAccessVo();
        ZpzOauthBeanUtils.copyProperties(obj,ret);
        return  ZpzOauthResultMsg.okForData(ret);
    }

    /**
     * 获取放行权限详情
     * */
    @Override
    public ZpzOauthResultMsg<GetAccessVo> getReleaseAccessDetail(HttpServletRequest request, GetAccessDetailQo getAccessDetailQo){
        if (StringUtils.isBlank(getAccessDetailQo.getId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameReleaseAccess obj = zpzOauthFrameReleaseAccessMapper.selectByPrimaryKey(getAccessDetailQo.getId());
        if (obj==null){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }
        GetAccessVo ret=new GetAccessVo();
        ZpzOauthBeanUtils.copyProperties(obj,ret);
        return  ZpzOauthResultMsg.okForData(ret);
    }

    /**
     * 添加权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> addAccess(HttpServletRequest request, InsertAccessQo insertAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        if (StringUtils.isBlank(insertAccessQo.getAccessCode()) || StringUtils.isBlank(insertAccessQo.getServiceId())
        || StringUtils.isBlank(insertAccessQo.getMethodUrl())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameAccess obj=new FrameAccess();
        obj.setAccessCode(insertAccessQo.getAccessCode());
        List<FrameAccess> list=zpzOauthFrameAccessMapper.selectFrameAccessList(obj);
        if (list!=null || list.size()>0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_EXISTCODE_31001);
        }

        FrameAccess frameAccess=new FrameAccess();
        frameAccess.setAccessCode(insertAccessQo.getAccessCode());
        frameAccess.setServiceId(insertAccessQo.getServiceId());
        frameAccess.setMethodUrl(insertAccessQo.getMethodUrl());
        frameAccess.setMethodType(insertAccessQo.getMethodType());
        frameAccess.setName(insertAccessQo.getName());
        frameAccess.setSetEn(insertAccessQo.getSetEn());
        frameAccess.setSetDe(insertAccessQo.getSetDe());
        frameAccess.setSetSign(insertAccessQo.getSetSign());
        frameAccess.setSetCheckSign(insertAccessQo.getSetCheckSign());
        frameAccess.setRemarks(insertAccessQo.getRemarks());
        frameAccess.setCreateDate(new Date());
        frameAccess.setCreateBy(ucodeLicense);

        Integer ret= zpzOauthFrameAccessMapper.insert(frameAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_ERROR_31000);
        }
    }

    /**
     * 添加放行权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> addReleaseAccess(HttpServletRequest request, InsertReleaseAccessQo insertReleaseAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        if (StringUtils.isBlank(insertReleaseAccessQo.getAccessCode()) || StringUtils.isBlank(insertReleaseAccessQo.getServiceId())
                || StringUtils.isBlank(insertReleaseAccessQo.getMethodUrl()) || StringUtils.isBlank(insertReleaseAccessQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameReleaseAccess obj=new FrameReleaseAccess();
        obj.setAccessCode(insertReleaseAccessQo.getAccessCode());
        obj.setClientId(insertReleaseAccessQo.getClientId());
        List<FrameReleaseAccess> list=zpzOauthFrameReleaseAccessMapper.selectFrameReleaseAccessList(obj);
        if (list!=null || list.size()>0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_EXISTCODE_31001);
        }

        FrameReleaseAccess frameReleaseAccess=new FrameReleaseAccess();
        frameReleaseAccess.setAccessCode(insertReleaseAccessQo.getAccessCode());
        frameReleaseAccess.setServiceId(insertReleaseAccessQo.getServiceId());
        frameReleaseAccess.setMethodUrl(insertReleaseAccessQo.getMethodUrl());
        frameReleaseAccess.setMethodType(insertReleaseAccessQo.getMethodType());
        frameReleaseAccess.setSetEn(insertReleaseAccessQo.getSetEn());
        frameReleaseAccess.setSetDe(insertReleaseAccessQo.getSetDe());
        frameReleaseAccess.setSetSign(insertReleaseAccessQo.getSetSign());
        frameReleaseAccess.setSetCheckSign(insertReleaseAccessQo.getSetCheckSign());
        frameReleaseAccess.setClientId(insertReleaseAccessQo.getClientId());
        frameReleaseAccess.setType(insertReleaseAccessQo.getType());
        frameReleaseAccess.setRemarks(insertReleaseAccessQo.getRemarks());
        frameReleaseAccess.setCreateDate(new Date());
        frameReleaseAccess.setCreateBy(ucodeLicense);

        Integer ret= zpzOauthFrameReleaseAccessMapper.insert(frameReleaseAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_ERROR_31000);
        }
    }

    /**
     * 修改权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> updateAccess(HttpServletRequest request, UpdateAccessQo updateAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        if (StringUtils.isBlank(updateAccessQo.getAccessCode()) || StringUtils.isBlank(updateAccessQo.getServiceId())
                || StringUtils.isBlank(updateAccessQo.getMethodUrl())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameAccess obj=new FrameAccess();
        obj.setAccessCode(updateAccessQo.getAccessCode());
        List<FrameAccess> list=zpzOauthFrameAccessMapper.selectFrameAccessList(obj);
        if (list==null || list.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ACCESS_NULL_31003);
        }

        FrameAccess frameAccess=new FrameAccess();
        frameAccess.setId(updateAccessQo.getId());
        frameAccess.setAccessCode(updateAccessQo.getAccessCode());
        frameAccess.setServiceId(updateAccessQo.getServiceId());
        frameAccess.setMethodUrl(updateAccessQo.getMethodUrl());
        frameAccess.setMethodType(updateAccessQo.getMethodType());
        frameAccess.setName(updateAccessQo.getName());
        frameAccess.setSetEn(updateAccessQo.getSetEn());
        frameAccess.setSetDe(updateAccessQo.getSetDe());
        frameAccess.setSetSign(updateAccessQo.getSetSign());
        frameAccess.setSetCheckSign(updateAccessQo.getSetCheckSign());
        frameAccess.setRemarks(updateAccessQo.getRemarks());
        frameAccess.setUpdateDate(new Date());
        frameAccess.setUpdateBy(ucodeLicense);

        Integer ret= zpzOauthFrameAccessMapper.updateFrameAccess(frameAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.UPDATE_ACCESS_FAILURE_31004);
        }
    }

    /**
     * 修改放行权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> updateReleaseAccess(HttpServletRequest request, UpdateReleaseAccessQo updateReleaseAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        if (StringUtils.isBlank(updateReleaseAccessQo.getAccessCode()) || StringUtils.isBlank(updateReleaseAccessQo.getServiceId())
                || StringUtils.isBlank(updateReleaseAccessQo.getMethodUrl())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        FrameReleaseAccess obj=new FrameReleaseAccess();
        obj.setAccessCode(updateReleaseAccessQo.getAccessCode());
        obj.setClientId(updateReleaseAccessQo.getClientId());
        List<FrameReleaseAccess> list=zpzOauthFrameReleaseAccessMapper.selectFrameReleaseAccessList(obj);
        if (list==null || list.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ACCESS_NULL_31003);
        }

        FrameReleaseAccess frameReleaseAccess=new FrameReleaseAccess();
        frameReleaseAccess.setId(updateReleaseAccessQo.getId());
        frameReleaseAccess.setAccessCode(updateReleaseAccessQo.getAccessCode());
        frameReleaseAccess.setServiceId(updateReleaseAccessQo.getServiceId());
        frameReleaseAccess.setMethodUrl(updateReleaseAccessQo.getMethodUrl());
        frameReleaseAccess.setMethodType(updateReleaseAccessQo.getMethodType());
        frameReleaseAccess.setSetEn(updateReleaseAccessQo.getSetEn());
        frameReleaseAccess.setSetDe(updateReleaseAccessQo.getSetDe());
        frameReleaseAccess.setSetSign(updateReleaseAccessQo.getSetSign());
        frameReleaseAccess.setSetCheckSign(updateReleaseAccessQo.getSetCheckSign());
        frameReleaseAccess.setClientId(updateReleaseAccessQo.getClientId());
        frameReleaseAccess.setType(updateReleaseAccessQo.getType());
        frameReleaseAccess.setRemarks(updateReleaseAccessQo.getRemarks());
        frameReleaseAccess.setUpdateDate(new Date());
        frameReleaseAccess.setUpdateBy(ucodeLicense);

        Integer ret= zpzOauthFrameReleaseAccessMapper.updateFrameReleaseAccess(frameReleaseAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.UPDATE_ACCESS_FAILURE_31004);
        }
    }

    /**
     * 删除权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> delAccess(HttpServletRequest request, DeleteAccessQo deleteAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        FrameAccess obj=new FrameAccess();
        obj.setId(deleteAccessQo.getId());
        List<FrameAccess> list=zpzOauthFrameAccessMapper.selectFrameAccessList(obj);
        if (list==null || list.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ACCESS_NULL_31003);
        }
        FrameAccess frameAccess=new FrameAccess();
        frameAccess.setId(deleteAccessQo.getId());
        frameAccess.setUpdateBy(ucodeLicense);
        frameAccess.setUpdateDate(new Date());
        Integer ret=zpzOauthFrameAccessMapper.delFrameAccess(frameAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DEL_ACCESS_FAILURE_31002);
        }
    }

    /**
     * 删除放行权限信息
     * */
    @Override
    public ZpzOauthResultMsg<Boolean> delReleaseAccess(HttpServletRequest request, DeleteAccessQo deleteAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        FrameReleaseAccess obj=new FrameReleaseAccess();
        obj.setId(deleteAccessQo.getId());
        List<FrameReleaseAccess> list=zpzOauthFrameReleaseAccessMapper.selectFrameReleaseAccessList(obj);
        if (list==null || list.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ACCESS_NULL_31003);
        }
        FrameReleaseAccess frameReleaseAccess=new FrameReleaseAccess();
        frameReleaseAccess.setId(deleteAccessQo.getId());
        frameReleaseAccess.setUpdateBy(ucodeLicense);
        frameReleaseAccess.setUpdateDate(new Date());
        Integer ret=zpzOauthFrameReleaseAccessMapper.delFrameReleaseAccess(frameReleaseAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DEL_ACCESS_FAILURE_31002);
        }
    }
}
